import 'package:dio/dio.dart';
import 'package:flutter_application_1/global.dart';
import 'package:flutter_application_1/models/LocationPoi.dart';

import 'package:flutter_application_1/request/request.dart';

Request request = Request();

Dio dio = Dio();

const Map<String, dynamic> AMAP_MAP = {
  // 每次调用接口都带上key
  'key': Global.MAP_WEB_KEY,
  // 返回全部信息
  'extensions': 'all',
  'datatype': 'poi',
};

// 坐标解析地址
Future<Map<String, dynamic>?> geocodeRegeo({
  required double longitude,
  required double latitude,
}) async {
  Map<String, dynamic> data = {};
  data.addEntries(AMAP_MAP.entries);
  data['location'] = '$longitude,$latitude';

  Response response = await dio.get(
    Global.MAP_WEB_API + '/v3/geocode/regeo',
    queryParameters: data,
  );

  return response.data;
}

/// 地址解析坐标
/// city: 城市名称或者城市编码
/// 城市编码表：https://lbs.amap.com/api/webservice/download
Future<Map<String, dynamic>?> geocodeGeo({
  required String address,
  String city = '',
}) async {
  Map<String, dynamic> data = {};
  data.addEntries(AMAP_MAP.entries);
  data['address'] = address;
  data['city'] = city;

  Response response = await dio.get(
    Global.MAP_WEB_API + '/v3/geocode/geo',
    queryParameters: data,
  );

  Map<String, dynamic> result = {};

  if (response.data['geocodes'] != null &&
      response.data['geocodes'] is List &&
      response.data['geocodes'].length > 0) {
    Map geo = response.data['geocodes'][0];
    String location = geo['location'];
    List<String> locationArray = location.split(',');
    if (locationArray.length == 2) {
      result = {
        'longitude': locationArray[0],
        'latitude': locationArray[1],
      };
    }
  }

  return result;
}

// 获取周围
Future<List<LocationPoi>> getAroundPois({
  required double longitude,
  required double latitude,
  String keywords = '',
  int size = 20,
  int page = 1,
}) async {
  Map<String, dynamic> data = {};
  data.addEntries(AMAP_MAP.entries);
  data['location'] = '$longitude,$latitude';
  data['keywords'] = keywords;
  data['page_size'] = size;
  data['page_num'] = page;

  List<LocationPoi> result = [];

  Response response = await dio.get(
    Global.MAP_WEB_API + '/v5/place/around',
    queryParameters: data,
  );

  Map responseData = response.data;

  if (int.parse(responseData['status']) == 1 && responseData['pois'] is List) {
    List<dynamic> pois = responseData['pois'] as List<dynamic>;
    pois.forEach((element) {
      Map<String, dynamic> e = Map<String, dynamic>.from(element);
      result.add(LocationPoi.fromJson(e));
    });
  }

  return result;
}

/// 搜索
/// city: 城市范围支持输入 城市名称 和 城市编码
/// 城市编码表：https://lbs.amap.com/api/webservice/download
/// types: 指定地点类型，多个用 | 分隔
/// POI分类编码：https://lbs.amap.com/api/webservice/download
Future<List<LocationPoi>> search({
  String keywords = '',
  String types = '',
  String city = '',
  int size = 20,
  int page = 1,
}) async {
  Map<String, dynamic> data = {};
  data.addEntries(AMAP_MAP.entries);
  data['keywords'] = keywords;
  data['types'] = types;
  data['page_size'] = size;
  data['page_num'] = page;
  data['region'] = city;

  List<LocationPoi> result = [];

  Response response = await dio.get(
    Global.MAP_WEB_API + '/v5/place/text',
    queryParameters: data,
  );

  Map responseData = response.data;

  if (int.parse(responseData['status']) == 1 && responseData['pois'] is List) {
    List<dynamic> pois = responseData['pois'] as List<dynamic>;
    pois.forEach((element) {
      Map<String, dynamic> e = Map<String, dynamic>.from(element);
      result.add(LocationPoi.fromJson(e));
    });
  }

  return result;
}
